#include <stdlib.h>
#include <stdio.h>
#include <spu_mfcio.h>
#include "../ubicus.h"


/* allocating the control_block structure on the stack.  We align
 * it by  */
control_block_t control_block __attribute__ ((aligned (128)));




int main(unsigned long long speid __attribute__ ((unused)),
         unsigned long long argp, 
         unsigned long long envp __attribute__ ((unused)))
{
  unsigned int tag;
  int i;


  /* First, we reserve a MFC tag for use */
  tag = mfc_tag_reserve();
  if (tag == MFC_TAG_INVALID)
  {
    printf ("SPU ERROR, unable to reserve tag\n");
    return 1;
  }
  
/*  printf("%d", sizeof(control_block_t)); */
/* DMA the control block information from system memory */
  
    mfc_get (&control_block, argp, sizeof (control_block_t), tag, 0, 0);

/*    wait for the DMA to complete */
    mfc_write_tag_mask (1 << tag);
    mfc_read_tag_status_all ();

  vector float x = {control_block.x0, control_block.x0, control_block.x0, control_block.x0};
  
  vector float y = {control_block.y0, control_block.y0, control_block.y0, control_block.y0};
  
  vector float a = {1.0, 2.0, 3.0, 4.0};
  
  vector float b = {1.0, 2.0, 3.0, 4.0};
  
  for (i = 0; i < 50000; i++)
  {
  	x = spu_mul(x, x);
  	x = spu_mul(x, a);
  	x = spu_sub(x, b);
    	y = spu_mul(y, y);
  	y = spu_mul(y, a);
  	y = spu_sub(y, b);
  }
  printf("vecteur_x%d = {%f, %f, %f, %f}\n",control_block.id, spu_extract(x, 0), spu_extract(x, 1), spu_extract(x, 2), spu_extract(x, 3));
  printf("vecteur_y%d = {%f, %f, %f, %f}\n",control_block.id, spu_extract(y, 0), spu_extract(y, 1), spu_extract(y, 2), spu_extract(y, 3));

  return 0;
}
